package com.axxy.factory;

import java.util.Optional;

/**
 * @author 一缕茜阳
 * @date 2021/7/27
 */
public class SimpleFactoryDemo {
    public static void main(String[] args) {
        SimpleFactory factory = new SimpleFactory();
        Optional<Product> optional1 = Optional.ofNullable(factory.makeProduct("A"));
        Optional<Product> optional2 = Optional.ofNullable(factory.makeProduct("B"));
        Optional<Product> optional3 = Optional.ofNullable(Optional.ofNullable(factory.makeProduct("C")).orElse(optional2.isPresent() ? optional2.get() : null));

        optional1.get().show();
        optional2.get().show();
        optional3.get().show();
    }
}
interface Product {
    void show();
}

class ProductA implements Product {

    @Override
    public void show() {
        System.out.println("ProductA");
    }
}

class ProductB implements Product {

    @Override
    public void show() {
        System.out.println("ProductB");
    }
}

final class Constant {
    static final String PRODUCT_A = "A";
    static final String PRODUCT_B = "B";
}

class SimpleFactory {
    public  Product makeProduct(String str) {
        switch (str) {
            case Constant.PRODUCT_A:
                return new ProductA();
            case Constant.PRODUCT_B:
                return new ProductB();
            default:
                return null;
        }
    }
}